﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using Zadani09.db;
using Zadani09.db.ODS;

namespace Zadani09.admin
{
    public partial class Vystavy : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            ObjectDataSource2.Updated += new ObjectDataSourceStatusEventHandler(ObjectDataSource2_Updated);
            ObjectDataSource2.Inserted += new ObjectDataSourceStatusEventHandler(ObjectDataSource2_Inserted);
            ObjectDataSource1.Deleted += new ObjectDataSourceStatusEventHandler(ObjectDataSource1_Deleted);
        }

        void ObjectDataSource1_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            //?
            ObjectDataSource4.Select();
            GridView2.DataBind();
        }


        void ObjectDataSource2_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            aktualizujData();
        }

        void ObjectDataSource2_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            aktualizujData();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //pridavani dila je schovano, zobrazeno muze byt pod jistymi podminkami
            schovejPridaniDila();
        }

        private void aktualizujData()
        {
            ObjectDataSource1.Select();
            GridView1.DataBind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            GridView1.DataBind();
            if (GridView1.Rows.Count <= 0)
            {
                DetailsView1.ChangeMode(DetailsViewMode.Insert);
                schovejVyhledavani();
              //  schovejPridaniDila();
            }
            else if (DetailsView1.CurrentMode != DetailsViewMode.Edit &&
                     GridView1.SelectedIndex <= 0 &&
                     DetailsView1.DataItemIndex >= 0)
            {
                DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);

                GridView1.SelectedIndex = 0;
                ObjectDataSource2.Select();
                DetailsView1.DataBind();

                DetailsView1.PageIndex = 0;
                zobrazVyhledavani();
            }

            if (GridView1.Rows.Count > 0) //zapni vyhledavani vzdy kdyz je co hledat..
            {
                zobrazVyhledavani();
            }

            ObjectDataSource5.Select();
            DropDownList5.DataBind();

            if (GridView1.Rows.Count > 0 && DropDownList5.Items.Count > 0) zobrazPridaniDila();

            base.OnPreRender(e);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            ObjectDataSource5.Insert();
            
            //na serveru to dela haluze, ale na lokalu se mi to neobnovuje, cili ted nevim
            //jestli dobre chodi i vystavy z pohledu umelce
            
            // dela haluze na serveru GridView2.DataBind();
            // dela haluze na serveru DropDownList5.DataBind();

          //  if (DropDownList5.Items.Count <= 0) schovejPridaniDila();
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            Session["vyhl_podle_admin_vystavy"] = IsPostBack ? DropDownList1.SelectedValue : DropDownList1.Items[0].Value;

            base.OnPreRenderComplete(e);
        }

        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetCompletionList(string prefixText, int count, string contextKey)
        {
            VystavaDataObject vystavy = new VystavaDataObject();
            List<Vystava> vysl;
            List<string> vrat = new List<string>();

            vysl = vystavy.cti_s_vyhledavanim(null, prefixText,
                        (string)HttpContext.Current.Session["vyhl_podle_admin_vystavy"]);

            foreach (Vystava jeden in vysl)
                vrat.Add(jeden[HttpContext.Current.Session["vyhl_podle_admin_vystavy"].ToString()].ToString());

            return (vrat.ToArray());
        }


        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox1.Text = ViewState["vyhl_admin_vystavy"] == null ? "" : (string)ViewState["vyhl_admin_vystavy"];
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ViewState["vyhl_admin_vystavy"] = TextBox1.Text;
        }

        private void schovejVyhledavani()
        {
            UpdatePanel1.Visible = false;
            Button1.Visible = false;
            DropDownList1.Visible = false;
        }

        private void zobrazVyhledavani()
        {
            UpdatePanel1.Visible = true;
            Button1.Visible = true;
            DropDownList1.Visible = true;
        }

        private void schovejPridaniDila()
        {
            Panel1.Visible = false;
            DropDownList5.Visible = false;
            Button2.Visible = false;
        }

        private void zobrazPridaniDila()
        {
            Panel1.Visible = true;
            DropDownList5.Visible = true;
            Button2.Visible = true;
        }

    }
}